package com.kalvan.db.config;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;

/**
 * 安全数据源
 *
 * @author chenliang
 */
@Slf4j
public class DruidSecretBasicDataSource extends DruidDataSource {
    private static final long serialVersionUID = 1L;
    public static final String DEFAULT_KEY = "kalvan88";

    public static String decryptDb(String info) {
        try {
            return SecureUtil.des(DEFAULT_KEY.getBytes()).decryptStr(info);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return info;
        }
    }

    @Override
    public synchronized void setPassword(String password) {
        String newPassword = decryptDb(password);
        super.setPassword(newPassword);
    }

    @Override
    public synchronized void setUrl(String url) {
        super.setUrl(url);
    }

    @Override
    public synchronized void setUsername(String username) {
        String newUsername = decryptDb(username);
        super.setUsername(newUsername);
    }
}
